package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    /**
     * 订单分页
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    @DateTimeFormat
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("订单分页数据 page：{},pageSize:{},number:{},beginTime:{},endTime:{}", page, pageSize, number, beginTime, endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        if (beginTime != null && endTime != null) {
            queryWrapper.between(Orders::getOrderTime,beginTime,endTime);
        }
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(ordersPage, queryWrapper);
        //对象拷贝,需要排除records属性，因为改集合的数据不正确，需要手动处理
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        //处理分页集合数据
        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> list = records.stream().map((item) -> {
            //将每一个Dish对象，拷贝到对应OrdersDto对象中
            OrdersDto ordersDto = new OrdersDto();
            //对象拷贝
            BeanUtils.copyProperties(item, ordersDto);
            //
            Long userId = item.getUserId();//用户id
            //根据id查询订单对象
            User user = userService.getById(userId);

            //给ordersDto的categoryName赋值
            if (user != null) {
                String userName = user.getName();
                ordersDto.setUserName(userName);
            }
            return ordersDto;
        }).collect(Collectors.toList());
        //将List集合，添加到分页对象中
        ordersDtoPage.setRecords(list);

        return R.success(ordersDtoPage);
    }


    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("修改成功");
    }
}